/**
 * Element Plus 图标工具函数
 */

// Element Plus 图标分类和列表
const ELEMENT_PLUS_ICONS = {
  system: {
    name: 'system',
    label: '系统图标',
    icons: [
      'Plus', 'Minus', 'CirclePlus', 'Search', 'Female', 'Male', 'Aim', 'House',
      'FullScreen', 'Loading', 'Link', 'Service', 'Pointer', 'Star', 'Notification',
      'Connection', 'ChatDotRound', 'Setting', 'Clock', 'Position', 'Discount',
      'Odometer', 'ChatSquare', 'ChatRound', 'ChatLineRound', 'ChatLineSquare',
      'ChatDotSquare', 'View', 'Hide', 'Unlock', 'Lock', 'RefreshRight', 'RefreshLeft',
      'Refresh', 'Bell', 'MuteNotification', 'User', 'Check', 'CircleCheck', 'Warning',
      'CircleClose', 'Close', 'PieChart', 'More', 'Compass', 'Filter', 'Switch',
      'Select', 'SemiSelect', 'CloseBold', 'EditPen', 'Edit', 'Message', 'MessageBox',
      'TurnOff', 'Finished', 'Delete', 'Crop', 'SwitchButton', 'Operation', 'Open',
      'Remove', 'ZoomOut', 'ZoomIn', 'InfoFilled', 'CircleCheckFilled', 'SuccessFilled',
      'WarningFilled', 'CircleCloseFilled', 'QuestionFilled', 'WarnTriangleFilled',
      'UserFilled', 'MoreFilled', 'Tools', 'HomeFilled', 'Menu', 'UploadFilled',
      'Avatar', 'HelpFilled', 'Share', 'StarFilled', 'Comment', 'Histogram', 'Grid',
      'Promotion', 'DeleteFilled', 'RemoveFilled', 'CirclePlusFilled'
    ]
  },
  direction: {
    name: 'direction',
    label: '方向图标',
    icons: [
      'ArrowLeft', 'ArrowUp', 'ArrowRight', 'ArrowDown', 'ArrowLeftBold', 'ArrowUpBold',
      'ArrowRightBold', 'ArrowDownBold', 'DArrowRight', 'DArrowLeft', 'Download',
      'Upload', 'Top', 'Bottom', 'Back', 'Right', 'TopRight', 'TopLeft', 'BottomRight',
      'BottomLeft', 'Sort', 'SortUp', 'SortDown', 'Rank', 'CaretLeft', 'CaretTop',
      'CaretRight', 'CaretBottom', 'DCaret', 'Expand', 'Fold'
    ]
  },
  document: {
    name: 'document',
    label: '文档图标',
    icons: [
      'DocumentAdd', 'Document', 'Notebook', 'Tickets', 'Memo', 'Collection',
      'Postcard', 'ScaleToOriginal', 'SetUp', 'DocumentDelete', 'DocumentChecked',
      'DataBoard', 'DataAnalysis', 'CopyDocument', 'FolderChecked', 'FolderDelete',
      'FolderAdd', 'Folder', 'FolderOpened', 'Files', 'Reading', 'CopyDocument',
      'Management', 'TakeawayBox', 'Paperclip', 'DocumentCopy', 'FolderRemove'
    ]
  },
  media: {
    name: 'media',
    label: '媒体图标',
    icons: [
      'VideoPlay', 'VideoPause', 'Microphone', 'MicrophoneSlash', 'Camera',
      'CameraFilled', 'Picture', 'PictureFilled', 'PictureRounded', 'Headset',
      'Film', 'Promotion', 'VideoCamera', 'VideoCameraFilled', 'Headset',
      'MagicStick', 'Brush', 'BrushFilled', 'Crop', 'ScaleToOriginal'
    ]
  },
  weather: {
    name: 'weather',
    label: '天气图标',
    icons: [
      'Sunny', 'PartlyCloudy', 'Cloudy', 'Lightning', 'Drizzling', 'Raining',
      'Pouring', 'Storm', 'SnowingOne', 'Snowing', 'MostlyCloudy', 'Foggy',
      'Hazy', 'WindPower', 'Sunrise', 'Sunset', 'Moon', 'MoonNight'
    ]
  },
  food: {
    name: 'food',
    label: '食物图标',
    icons: [
      'Apple', 'Orange', 'Pear', 'Cherry', 'Watermelon', 'Grape', 'Strawberry',
      'Lollipop', 'IceCream', 'IceCreamSquare', 'IceCreamRound', 'IceTea',
      'Milk', 'Coffee', 'CoffeeCup', 'WineGlass', 'Goblet', 'GobletFull',
      'GobletSquare', 'GobletSquareFull', 'Bowl', 'Dish', 'DishDot', 'Burger',
      'ForkSpoon', 'KnifeFork', 'Chicken', 'FriedChicken', 'HotDog'
    ]
  },
  transport: {
    name: 'transport',
    label: '交通图标',
    icons: [
      'Car', 'Bicycle', 'Ship', 'Truck', 'Van', 'TruckFilled', 'Bus',
      'Train', 'Subway', 'Airplane', 'Rocket', 'Sailboat', 'Motor'
    ]
  },
  electronic: {
    name: 'electronic',
    label: '电子设备',
    icons: [
      'Iphone', 'Cellphone', 'Computer', 'Monitor', 'Laptop', 'Keyboard',
      'Mouse', 'Headset', 'Microphone', 'Camera', 'VideoCamera', 'Printer',
      'Scanner', 'Fax', 'Phone', 'PhoneFilled', 'Cpu', 'MemoryCard',
      'HardDisk', 'UDisk', 'SdCard', 'Usb'
    ]
  },
  shopping: {
    name: 'shopping',
    label: '购物图标',
    icons: [
      'ShoppingBag', 'ShoppingCart', 'ShoppingCartFull', 'ShoppingTrolley',
      'Goods', 'GoodsFilled', 'Box', 'Present', 'PresentFilled', 'Sell',
      'SoldOut', 'Discount', 'PriceTag', 'Coin', 'Money', 'Wallet',
      'WalletFilled', 'CreditCard', 'BankCard'
    ]
  },
  sports: {
    name: 'sports',
    label: '体育图标',
    icons: [
      'Football', 'Basketball', 'Soccer', 'Baseball', 'Tennis', 'Volleyball',
      'TableTennis', 'Bowling', 'Golf', 'Trophy', 'TrophyBase', 'Medal',
      'Flag', 'Bicycle', 'Skateboard', 'Swimming'
    ]
  },
  office: {
    name: 'office',
    label: '办公图标',
    icons: [
      'Briefcase', 'Suitcase', 'SuitcaseLine', 'OfficeBuilding', 'School',
      'Reading', 'Notebook', 'EditPen', 'Pen', 'Brush', 'Stamp', 'Paperclip',
      'Files', 'Folder', 'FolderOpened', 'Calendar', 'AlarmClock', 'Timer',
      'Stopwatch', 'Clock'
    ]
  }
}

/**
 * 获取 Element Plus 图标分类列表
 * @returns 图标分类数组
 */
export function getElementPlusIcons() {
  return Object.values(ELEMENT_PLUS_ICONS).map(category => ({
    name: category.name,
    label: category.label,
    icons: category.icons
  }))
}

/**
 * 获取所有 Element Plus 图标名称
 * @returns 图标名称数组
 */
export function getAllElementPlusIconNames() {
  return Object.values(ELEMENT_PLUS_ICONS).flatMap(category => category.icons)
}

/**
 * 根据关键词搜索图标
 * @param keyword 搜索关键词
 * @param icons 图标列表（可选，默认使用所有图标）
 * @returns 匹配的图标名称数组
 */
export function searchIcons(keyword, icons) {
  const searchList = icons || getAllElementPlusIconNames()
  const lowerKeyword = keyword.toLowerCase()
  
  return searchList.filter(icon => 
    icon.toLowerCase().includes(lowerKeyword)
  )
}

/**
 * 检查图标是否存在
 * @param iconName 图标名称
 * @returns 是否存在
 */
export function isElementPlusIcon(iconName) {
  return getAllElementPlusIconNames().includes(iconName)
}

/**
 * 根据图标名称获取所属分类
 * @param iconName 图标名称
 * @returns 分类信息或 null
 */
export function getIconCategory(iconName) {
  for (const category of Object.values(ELEMENT_PLUS_ICONS)) {
    if (category.icons.includes(iconName)) {
      return {
        name: category.name,
        label: category.label
      }
    }
  }
  return null
}

/**
 * 获取推荐图标（常用图标）
 * @returns 推荐图标列表
 */
export function getRecommendedIcons() {
  return [
    'Plus', 'Minus', 'Search', 'Edit', 'Delete', 'Check', 'Close',
    'ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown', 'Upload', 'Download',
    'User', 'Setting', 'House', 'Star', 'Heart', 'Bell', 'Message',
    'Camera', 'Picture', 'Document', 'Folder', 'Calendar', 'Clock'
  ]
}

/**
 * 图标名称格式化
 * @param iconName 原始图标名称
 * @returns 格式化后的显示名称
 */
export function formatIconName(iconName) {
  // 将驼峰命名转换为带空格的标题格式
  return iconName
    .replace(/([A-Z])/g, ' $1')
    .replace(/^./, str => str.toUpperCase())
    .trim()
}